<?php

namespace lico\Tool;

class Upload
{

    protected $type = 'single';
    protected $config = [
        'allowExt' => ['image/gif', 'image/jpg', 'image/jpeg', 'image/png', 'image/wbmp'], //允许的文件格式
        'maxSize' => 1, //一次请求限制的最大字节数，单位：M
        'saveFolder' => '', //存储的文件夹名
        'sliceNum' => 1, //切片编号
        'sliceTotal' => 10, //切片总数
        'sliceUid' => '', //切片上传的唯一值,隔离上传文件批次
    ];

    protected $handler;
    protected $error;
    protected $ext;

    public function __construct($type = '', $config = [])
    {
        $type && $this->type = $type;
        $config && $this->config = array_merge($this->config, $config);
        $type = ucfirst(strtolower($this->type));
        $namespace = __CLASS__ . '\\' . $type;
        $this->handler = new $namespace($this->config);
    }

    /**
     * 获取上传类
     */
    public function getHandler()
    {
        return $this->handler;
    }

    /**
     * 上传保存
     */
    public function save()
    {
        $file = $_FILES;
        p($file);
        $success = $this->handler->checkFile($file);
        if (!$success) {
            return false;
        }
        p($this->config);

        $filename = $this->handler->save();

        return $filename;
    }
}
